﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;
using Common;

public partial class User_KaoQin_Add : System.Web.UI.Page
{
    protected int KqKey;

    protected string MenuType
    {
        get { return string.IsNullOrEmpty(Request["MenuType"]) ? "" : Request["MenuType"].ToString(); }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        KqKey = string.IsNullOrEmpty(Request["KqKey"]) ? 0 : Convert.ToInt32(Request["KqKey"]);

        if (!this.IsPostBack)
        {
            txt_StartTimeD.Attributes.Add("onfocus", "selectDate(this)");
            txt_StartTimeT.Attributes.Add("onfocus", "selectTime(this)");
            txt_EndTimeD.Attributes.Add("onfocus", "selectDate(this)");
            txt_EndTimeT.Attributes.Add("onfocus", "selectTime(this)");

            IList<Sys_Account>  UserList = new Account().GetNotAccount(1);
            foreach (Sys_Account account in UserList)
                txt_MUserKey.Items.Add(new ListItem("◆ "+account.RealName,account.UserKey.ToString()));

            if (KqKey != 0)
            {   
                Dat_KaoQin kaoqin = new KaoQin().SelectKaoQin(KqKey);
                txt_KqType.SelectedValue = kaoqin.KqType;
                txt_MUserKey.SelectedValue = kaoqin.MUserKey.ToString();
                txt_DanNum.Text = kaoqin.DanNum;
                txt_StartTimeD.Text = kaoqin.StartTime.ToString("yyyy-MM-dd");
                txt_StartTimeT.Text = kaoqin.StartTime.ToString().Split(' ')[1];
                txt_EndTimeD.Text = kaoqin.EndTime.ToString("yyyy-MM-dd");
                txt_EndTimeT.Text = kaoqin.EndTime.ToString().Split(' ')[1];
                txt_Reason.Text = kaoqin.Reason;

                btnSubmit.Text = "修 改";
            }

       }
    }

    //添加
    protected void btnSubmit_Click(object sender, EventArgs e)
    {

        DateTime starttime = Convert.ToDateTime(txt_StartTimeD.Text.Trim() + " " + txt_StartTimeT.Text.Trim());
        DateTime endtime = Convert.ToDateTime(txt_EndTimeD.Text.Trim() + " " + txt_EndTimeT.Text.Trim());

       

        if (KqKey == 0)
        {
            Dat_KaoQin kaoqin = new Dat_KaoQin
            {
                KqType = txt_KqType.SelectedValue,
                MUserKey = Convert.ToInt32(txt_MUserKey.SelectedValue),
                StartTime = starttime,
                EndTime = endtime,
                Reason = txt_Reason.Text.Trim(),
                SpanTimeD = WebUtility.GetTimeSpanD(starttime, endtime),
                SpanTimeH = WebUtility.GetTimeSpanH(starttime, endtime),
                SpanTimeM = WebUtility.GetTimeSpanM(starttime, endtime),
                CreateTime = DateTime.Now,
                UserKey = Convert.ToInt16(Session["UserKey"])
            };

            if (new KaoQin().AddKaoQin(kaoqin))
            {
                WebUtility.JsAlertToGo("添加考勤成功", "KaoQin_Add.aspx?menutype=221");
            }
        }
        else
        {
            Dat_KaoQin kaoqin = new Dat_KaoQin
            { 
                KqKey = KqKey,
                KqType = txt_KqType.SelectedValue,
                MUserKey = Convert.ToInt32(txt_MUserKey.SelectedValue),
                DanNum = txt_DanNum.Text.Trim(),
                StartTime = starttime,
                EndTime = endtime,
                Reason = txt_Reason.Text.Trim(),
                SpanTimeD = WebUtility.GetTimeSpanD(starttime, endtime),
                SpanTimeH = WebUtility.GetTimeSpanH(starttime, endtime),
                SpanTimeM = WebUtility.GetTimeSpanM(starttime, endtime)
                //CreateTime = DateTime.Now,
                //UserKey = 6
            };

            if (new KaoQin().EditKaoQin(kaoqin))
            {
                WebUtility.JsAlertToGo("修改考勤成功", "KaoQin.aspx?menutype=222");
            }
        }
    }
}
